package com.higgins.eventtracking.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.higgins.eventtracking.client.dto.MealDTO;
import com.higgins.eventtracking.server.dataModel.HealthEvent;
import com.higgins.eventtracking.server.dataModel.TrackingEvent;

@SuppressWarnings("serial")
public class EventServlet extends HttpServlet	{
	private static final Logger log = Logger.getLogger(EventServlet.class.getName());
	private List healthEvent = new ArrayList<String>();
	private List exercises = new ArrayList<String>();
	
	public EventServlet()	{
		healthEvent.add("Brush Teeth");
		healthEvent.add("Dump");
		healthEvent.add("Social");
		healthEvent.add("Reading");
		healthEvent.add("Sleep Session");
		
		exercises.add("Stretching");
		exercises.add("Squats");
		exercises.add("Leg Extension");
		exercises.add("Leg Curls");
		exercises.add("Abs");
		exercises.add("Lower Back");
		exercises.add("Delts Rear");
		exercises.add("Delts Butterfly");
		exercises.add("Delts Vertical");
		exercises.add("Bicep Curls");
		exercises.add("Triceps Exts");
		exercises.add("Upper Back");
		exercises.add("Walking");
		exercises.add("Cycling");
		exercises.add("Running");
		exercises.add("Swimming");
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)	throws IOException	{
		DataStoreService dataStore = new DataStoreService();
		DietTrackingDataService dietDataService = new DietTrackingDataService();
		dietDataService.openPersistenceManager();
		dataStore.openPersistenceManager();
		String event = req.getParameter("event");
		String eventType = req.getParameter("eventtype");
		String distanceStr = req.getParameter("distance");
		String weightStr = req.getParameter("weight");
		String repStr = req.getParameter("rep");
		String intensityStr = req.getParameter("intensity");
		String caloriesStr = req.getParameter("calories");
		String proteinStr = req.getParameter("protein");
		String fatStr = req.getParameter("fat");
		String waterStr = req.getParameter("water");
		String mealStr = req.getParameter("meal");
		String mealTypeStr = req.getParameter("meal type");
		
		Double distance = distanceStr!=null?Double.parseDouble(distanceStr.trim()):0.0;
		Double weight = weightStr!=null?Double.parseDouble(weightStr.trim()):0.0;
		Integer rep = repStr!=null?Integer.parseInt(repStr.trim()):0;
		Double calories = caloriesStr!=null?Double.parseDouble(caloriesStr.trim()):0.0;
		Double protein = caloriesStr!=null?Double.parseDouble(proteinStr.trim()):0.0;
		Double fat = fatStr!=null?Double.parseDouble(fatStr.trim()):0.0;
		Double water = waterStr!=null?Double.parseDouble(waterStr.trim()):0.0;

		if (event.equals("insert meal") && mealStr!=null && mealTypeStr!=null)	{
			dietDataService.insertMeal(mealStr, mealTypeStr, calories, protein, fat, water);
		}
		else if (event.equals("diet"))	{
			if (mealStr!=null)	{
				List<MealDTO> meals = dietDataService.retrieveMeals();
				for (MealDTO meal:meals)	{
					if (mealStr.equals(meal.getMeal()))	{
						dietDataService.insertConsumption(new Date(), meal.getMeal(), meal.getCalories(), 
								meal.getProtein(), meal.getFat(), meal.getWater());
					}
				}
			}	else	{
				dietDataService.insertConsumption(new Date(), "", calories, protein, fat, water);
			}
		} else if (healthEvent.contains(event) || eventType.toLowerCase().equals("healthevent"))	{
			HealthEvent healthEvent = new HealthEvent();
			healthEvent.setTimestamp(new Date());
			healthEvent.setHealthEventType(event);
			healthEvent.setHealthEventIntensity(intensityStr);
			dataStore.insertHealthEvent(healthEvent);
		} else if (exercises.contains(event))	{
			TrackingEvent trackingEvent = new TrackingEvent(event, new Date(), distance, 
													0, 0, weight, rep);
			dataStore.insertTrackingEvent(trackingEvent);
		}
		dataStore.closePersistenceManager();
		dietDataService.closePersistenceManager();
	}
}
